package com.easa.dao;

import com.easa.entity.Allteacher;
import com.easa.entity.ShowTeacherMsg;
import com.easa.entity.Teachermsg;
import com.easa.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class TeacherMsgDaoImpl implements TeacherMsgDao{

    @Autowired
    private JdbcTemplate jdbcTemplate;

    //通过tid查找教师信息
    public ShowTeacherMsg findTeacherMsgByid(String tid){
        String sql = "SELECT tid,tname,professinalTitle,contactWay,status,instruction FROM teachermsg WHERE tid = ?";

        ShowTeacherMsg showTeachermsg;
        try {
            showTeachermsg = jdbcTemplate.queryForObject(sql, new Object[]{tid}, new BeanPropertyRowMapper<ShowTeacherMsg>(ShowTeacherMsg.class));
        }catch (Exception e){
            System.out.println(e);
            showTeachermsg = null;
        }
        return showTeachermsg;
    }



    /*更新用户查询状态*/
    public void setConditionsByTid(String name, String viewClassify, String checkWay, String semesterId, String weekid){
        String sql = "Update teachermsg set viewContional=?,checkWay=?,semesterId=?,weekid=? where tid=?";
        jdbcTemplate.update(sql, new Object[]{viewClassify, checkWay,semesterId,weekid,name});
    }

    /*注册数据插入*/
    public int insertTeacherMsg(Teachermsg teachermsg){
        if(findIsExistTeacher(teachermsg.getTid()))
            return 1;
        int status = 0;
        if(isExistTeacher(teachermsg.getTid()))
            status = 1;
        String sql = "INSERT INTO teachermsg(tid,password,tname,registerTime,role,status) VALUES(?,?,?,?,?,?)";
        Date date = new Date();// 获取当前时间
        try {
            jdbcTemplate.update(sql, new Object[]{teachermsg.getTid(), teachermsg.getPassword(),
                    teachermsg.getTname(),date,0,status});
        }catch (Exception e){
            System.out.println(e);
            return 2;
        }
        return 0;
    }

    //查询是否有所有的教师表中是否有该教师
    private boolean isExistTeacher(String tid) {

        String sql = "SELECT * FROM allteacher WHERE tid = ?";

        Allteacher tmsg;
        try {
            tmsg = jdbcTemplate.queryForObject(sql, new Object[]{tid}, new BeanPropertyRowMapper<Allteacher>(Allteacher.class));
        }catch (Exception e){
            System.out.println(e);
            tmsg = null;
        }
        if (tmsg == null){
            return false;
        } else
            return true;
    }

    //查找是否存在同账号的用户
    private boolean findIsExistTeacher(String tid) {
        String sql = "SELECT * FROM teachermsg WHERE tid = ?";

        Teachermsg tmsg;
        try {
            tmsg = jdbcTemplate.queryForObject(sql, new Object[]{tid}, new BeanPropertyRowMapper<Teachermsg>(Teachermsg.class));
        }catch (Exception e){
            System.out.println(e);
            tmsg = null;
        }
        if (tmsg == null){
            return false;
        } else
            return true;
    }

    //获取用户信息登录验证
    public int loginValidate(String userId, String password){
        String sql = "SELECT * FROM teachermsg WHERE tid = ?";

        Teachermsg tmsg;
        try {
            tmsg = jdbcTemplate.queryForObject(sql, new Object[]{userId}, new BeanPropertyRowMapper<Teachermsg>(Teachermsg.class));
        }catch (Exception e){
            System.out.println(e);
            tmsg = null;
        }
        if (tmsg == null)
            return 1;
        else if(tmsg.getStatus()==0)
            return 3;
        else if (tmsg.getPassword().equals(password))
            return 0;
        else
            return 2;
    }

    //个人资料更新
    public Boolean updateTeacherMsg(Teachermsg teachermsg){
        String sql = "Update teachermsg set tname = ?,contactWay = ?,professinalTitle = ? where tid=?";
        Boolean isSuccess;
        try {
            jdbcTemplate.update(sql, new Object[]{teachermsg.getTname(), teachermsg.getContactWay(),teachermsg.getProfessinalTitle(),teachermsg.getTid()});
            isSuccess = true;
        }catch (Exception e){
            System.out.println(e);
            isSuccess = false;
        }
        return isSuccess;
    }

    //管理员登录认证
    public int validateAdmin(String userId, String pass){
        String sql = "SELECT * FROM teachermsg WHERE tid = ? AND (role = ?  OR role = ?)";
        //role= 1为管理员 2为系统管理员
        Teachermsg tmsg;
        try {
            tmsg = jdbcTemplate.queryForObject(sql, new Object[]{userId,1,2}, new BeanPropertyRowMapper<Teachermsg>(Teachermsg.class));
        }catch (Exception e){
            System.out.println(e);
            tmsg = null;
        }
        if (tmsg == null)
            return 1;
        else if (tmsg.getPassword().equals(MD5Utils.md5Password(pass)))
            return 0;
        else
            return 2;
    }

    //查询获取教师信息
    public List<Teachermsg> searchTeacherMsg(String tname){
        StringBuilder sql = new StringBuilder("select * from teachermsg where (role=0 or role=1)");

        if(tname != null && !tname.equals("")){
            sql.append(" and (tid like '%"+tname+"%' or tname like '%"+tname+"%')");
        }

        List<Teachermsg> list;
        try {
            list =  jdbcTemplate.query(String.valueOf(sql), new Object[]{}, new BeanPropertyRowMapper(Teachermsg.class));
        }catch (Exception e){
            System.out.println(e);
            list = null;
        }
        return list;
    }

    //加入教师信息（后台）
    public int addTeacherMsg(Teachermsg teachermsg){
        if(findIsExistTeacher(teachermsg.getTid()))
            return 2;
        String sql = "INSERT INTO teachermsg(tid,password,tname,contactWay,professinalTitle,registerTime,role,instruction,status) VALUES(?,?,?,?,?,?,?,?,?)";
        Date date = new Date();// 获取当前时间
        try {
            jdbcTemplate.update(sql, new Object[]{teachermsg.getTid(), teachermsg.getPassword(),
                    teachermsg.getTname(),teachermsg.getContactWay(),teachermsg.getProfessinalTitle(),
                    date,teachermsg.getRole(),teachermsg.getInstruction(),0});
        }catch (Exception e){
            System.out.println(e);
            return 1;
        }
        return 0;
    }

    //后台获取教师详细信息
    public Teachermsg findTeacherDetailByAdmin(String tid){
        String sql = "SELECT * FROM teachermsg WHERE tid = ?";

        Teachermsg tmsg;
        try {
            tmsg = jdbcTemplate.queryForObject(sql, new Object[]{tid}, new BeanPropertyRowMapper<Teachermsg>(Teachermsg.class));
        }catch (Exception e){
            System.out.println(e);
            tmsg = null;
        }
        return tmsg;
    }

    //系统管理员更新教师详细信息
    public int updateTeacherMsgBySAdmin(Teachermsg teachermsg){
        String sql = "Update teachermsg set tname= ?,professinalTitle = ?,contactWay = ?,status = ?,instruction=?,role=? where tid=?";
        try {
            jdbcTemplate.update(sql, new Object[]{teachermsg.getTname(), teachermsg.getProfessinalTitle(),teachermsg.getContactWay(),teachermsg.getStatus(),teachermsg.getInstruction(),teachermsg.getRole(),teachermsg.getTid()});
            return 0;
        }catch (Exception e){
            System.out.println(e);
            return 1;
        }
    }

    //管理员更新教师详细信息
    public int updateTeacherMsgByAdmin(Teachermsg teachermsg){
        String sql = "Update teachermsg set tname= ?,professinalTitle = ?,contactWay = ?,status = ?,instruction=? where tid=?";
        try {
            jdbcTemplate.update(sql, new Object[]{teachermsg.getTname(), teachermsg.getProfessinalTitle(),teachermsg.getContactWay(),teachermsg.getStatus(),teachermsg.getInstruction(),teachermsg.getTid()});
            return 0;
        }catch (Exception e){
            System.out.println(e);
            return 1;
        }
    }

    //重置密码
    public boolean resetPassword(String tid){
        String sql = "Update teachermsg set password= ? where tid=?";
        try {
            jdbcTemplate.update(sql, new Object[]{MD5Utils.md5Password("123456"),tid});
            return true;
        }catch (Exception e){
            System.out.println(e);
            return false;
        }
    }

    /*修改密码*/
    public int modifyPassword(String tid, String newPassword){
        String sql = "Update teachermsg set password=? where tid=?";
        try {
            jdbcTemplate.update(sql, new Object[]{newPassword,tid});
            return 0;
        }catch (Exception e){
            System.out.println(e);
            return 1;
        }
    }
}
